package com.markus.code.exam.beikeP;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
 * Author:markusZhang
 * Date:Create in 2020/9/7 15:40
 * todo:
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int T = scanner.nextInt();
            while (T > 0) {
                //n个栅栏
                int n = scanner.nextInt();
                //m种颜料
                int m = scanner.nextInt();
                //每种颜料后面的k种不合适颜料
                int k = scanner.nextInt();
                int[][] matrix = new int[m][k];
                for (int i = 0; i < matrix.length; i++) {
                    for (int j = 0; j < matrix[i].length; j++) {
                        matrix[i][j] = scanner.nextInt();
                    }
                }
                int process = 0;
                for (int i = 1; i <= m; i++) {
                    process += process(matrix, m, n, 0, i);
                }
                System.out.println(process);
                T--;
            }
        }
    }
    private static int process(int[][] matrix, int m, int n, int index, int row) {
        if (index == n) {
            return 1;
        }
        int ans = 0;
        for (int i=0;i<m;i++){
            for (int j = 0; j < matrix[row - 1].length; j++) {
                if (matrix[row - 1][j] != i+1) {
                    ans += process(matrix, m, n, index + 1, matrix[row - 1][j]);
                }
            }
        }
        return ans;
    }
}


